<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true">
      <el-form-item label="关键字">
        <el-input v-model="keyword" placeholder="工艺品名称/工艺品备注" />
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="fetchData()">
        查询
      </el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <el-table :data="craftsList">
      <el-table-column label="工艺品" width="300" align="center">
        <template slot-scope="scope">
          <div>
            <img
              style="float: left"
              width="150px"
              height="150px"
              :src="scope.row.indexImgUrl"
            />
            <p
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              <el-tooltip
                class="item"
                effect="dark"
                :content="scope.row.name"
                placement="left"
              >
                <label>{{ scope.row.name }}</label>
              </el-tooltip>
            </p>
            <br />
            <br />
            <br />
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="exhibitionName"
        label="所属展览"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="exhibitionTime"
        label="展览时间"
        align="center"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="price"
        label="售卖价格"
        align="center"
      ></el-table-column>
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success" size="mini">
            展览中
          </el-tag>
          <el-tag v-if="scope.row.status === 2" type="warning" size="mini">
            拍卖中
          </el-tag>
          <el-tag v-if="scope.row.status === -1" type="danger" size="mini">
            已撤展
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === 1"
            type="primary"
            size="mini"
            @click="chezhan(scope.row.id)"
          >
            撤展
          </el-button>
          <el-button type="info" size="mini">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[2, 5, 10]"
      style="padding: 30px 0"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="changePageSize"
      @current-change="changeCurrentPage"
    />
  </div>
</template>

<script>
import craftsApi from '@/api/crafts'

export default {
  data() {
    return {
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 2, // 每页记录数
      keyword: '',
      craftsList: []
    }
  },

  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      craftsApi
        .getPageList(this.page, this.limit, this.keyword)
        .then(response => {
          this.craftsList = response.data.pageModel.list
          this.total = response.data.pageModel.total
          console.log(this.craftsList)
        })
    },

    resetData() {
      craftsApi.getPageList(this.page, this.limit, '').then(response => {
        this.craftsList = response.data.pageModel.list
        this.total = response.data.pageModel.total
        console.log(this.craftsList)

        this.keyword = ''
      })
    },

    // 撤展
    chezhan(id) {
      console.log(id)
      craftsApi.changeCraftsStatus(id, -1).then(response => {
        // 刷新数据
        this.fetchData()
      })
    },

    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.limit = size
      this.fetchData()
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page
      this.fetchData()
    }
  }
}
</script>
